import 'package:flutter_todolist/db/table/todo_table.dart';
import 'package:flutter_todolist/utils/logger.dart';
import 'package:sqflite_common/sqlite_api.dart';

class TaskTypeTable implements TodoTable {
  @override
  Database? database;

  final String taskTypeTable = "taskTypeTable";

  @override
  createTable() {
    return '''
      CREATE TABLE IF NOT EXISTS $taskTypeTable (
      typeId INTEGER PRIMARY KEY,
      title TEXT,
      color INTEGER,
      icon TEXT
      )
    ''';
  }

  @override
  deleteItem(id) async {
    final Database db = database!;
    try {
      await db.delete(
        taskTypeTable,
        where: "typeId = ?",
        whereArgs: [id]
      );
      logW("taskTypeTable删除成功");
    } catch (e){
      logE("$taskTypeTable删除出错: $e");
    }
  }

  @override
  Future insertItem(Map<String, Object?> item) async {
    final Database db = database!;
    try {
      await db.insert(taskTypeTable, item);
      logW("taskTypeTable插入成功");
    } catch (e){
      logE("插入出错: $e");
    }
  }

  // List<Map<String, dynamic>> test = await testTable.queryList(
  //   where: "mid = ?",
  //   whereArgs: [widget.mid],
  // );
  @override
  Future<List<Map<String, dynamic>>> queryList({
    bool distinct = true, // 是否是独特的，即是否不让重复
    List<String>? columns, // 需要查询的列
    String? where, // 查询条件 "age > ?"
    List<Object>? whereArgs, // 查询条件参数(补充where) [16]
    String? groupBy, // 按列分组
    String? having, // 给分组设置条件
    String? orderBy, // 按列排序 asc/desc
    int limit = 100, // 限制查询结果数量
    int offset = 0, // 跳过几条数据
  }) async {
    final Database db = database!;
    try {
      List<Map<String, dynamic>> result = await db.query(
        taskTypeTable,
        distinct: distinct,
        columns: columns,
        where: where,
        whereArgs: whereArgs,
        groupBy: groupBy,
        having: having,
        orderBy: orderBy,
        limit: limit,
        offset: offset,
      );
      return result;
    } catch (e){
      logE("查询出错: $e");
    }

    return [];
  }

  @override
  updateItem(tid, item) async {
    final Database db = database!;
    try {
      await db.update(
          taskTypeTable,
        item,
        where: "typeId = ?",
        whereArgs: [tid]
      );
      logW("taskTypeTable更新成功");
    } catch (e){
      logE("$taskTypeTable更新出错: $e");
    }
  }

}

// var taskModule = {
//   'tid': '',
//   'title': '',
//   // 'subTask': [],
//   'createTime': '',
//   'isDone': true,
//   'remind': true,
//   'type': 0
// };
class TaskType {
  TaskType({
    this.typeId,
    this.title,
    this.color,
    this.icon,
  });

  TaskType.fromJson(dynamic json) {
    typeId = json['typeId'];
    title = json['title'];
    color = json['color'];
    icon = json['icon'];
  }

  int? typeId;
  String? title;
  int? color;
  String? icon;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['typeId'] = typeId;
    map['title'] = title;
    map['color'] = color;
    map['icon'] = icon;
    return map;
  }

}